package com.matt.domain;

import com.aliyun.openservices.ons.api.Message;
import com.matt.commons.model.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/10/25
 * *******************************
 */
@Data
@Entity
@Table(name = "mq_message")
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class MQMessage extends Model {

    @Column(columnDefinition = "varchar(50) COMMENT '消息id' ")
    private String msgId;

    @Column(columnDefinition = "varchar(50) COMMENT '主题' ")
    private String topic;

    @Column(columnDefinition = "varchar(50) COMMENT '标签' ")
    private String tag;

    @Column(columnDefinition = "varchar(50) COMMENT '消息键' ")
    private String keyStr;

    @Column(columnDefinition = "varchar(255) COMMENT '内容' ")
    private String content;

    @Column(columnDefinition = "int DEFAULT 1 COMMENT '消费次数' ")
    private Integer consumerTimes = 1;

    @Column(columnDefinition = "int DEFAULT 0 COMMENT '状态：0初始化，1成功消费，2，消费失败' ")
    private Integer status = 0;

    @Column(columnDefinition = " datetime DEFAULT NOW() COMMENT '创建时间' ")
    private Date created = new Date();

    @Column(columnDefinition = " datetime DEFAULT NOW() COMMENT '最后消费时间' ")
    private Date lastConsumed = new Date();

    public static MQMessage of(Message message, Integer status){
        MQMessage mqMessage = new MQMessage();
        mqMessage.setMsgId(message.getMsgID());
        mqMessage.setTopic(message.getTopic());
        mqMessage.setTag(message.getTag());
        mqMessage.setKeyStr(message.getKey());
        mqMessage.setStatus(status);
        mqMessage.setContent(new String(message.getBody()));
        return mqMessage;
    }


}
